﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QPushButton>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsRectItem>

#include <QGraphicsScene>
#include <QGraphicsSimpleTextItem>
#include <QGraphicsTextItem>
#pragma execution_character_set("utf-8")


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->widget->updacanvasPixmap(&all_store);
//    this->on_tableWidget_itemSelectionChanged();

    ui->widget_2->updacanvasPixmap(&all_store);   //画图图案


    //文字输入
    void(QSpinBox::*spSignal1)(int) = &QSpinBox::valueChanged;
    connect(ui->spinBox,spSignal1,ui->horizontalSlider,&QSlider::setValue);
    connect(ui->horizontalSlider, &QSlider::valueChanged, ui->spinBox, &QSpinBox::setValue);

    void(QSpinBox::*spSignal2)(int) = &QSpinBox::valueChanged;
    connect(ui->spinBox_2,spSignal2,ui->horizontalSlider_2,&QSlider::setValue);
    connect(ui->horizontalSlider_2, &QSlider::valueChanged, ui->spinBox_2, &QSpinBox::setValue);

    void(QSpinBox::*spSignal3)(int) = &QSpinBox::valueChanged;
    connect(ui->spinBox_3,spSignal3,ui->horizontalSlider_3,&QSlider::setValue);
    connect(ui->horizontalSlider_3, &QSlider::valueChanged, ui->spinBox_3, &QSpinBox::setValue);

    //字间距
    void(QSpinBox::*spSignal7)(int) = &QSpinBox::valueChanged;
    connect(ui->spinBox_7,spSignal7,ui->horizontalSlider_4,&QSlider::setValue);
    connect(ui->horizontalSlider_4, &QSlider::valueChanged, ui->spinBox_7, &QSpinBox::setValue);

    //字体
    QStringList strList;
    //strList<<"Microsoft YaHei";
    QFontDatabase database;
    foreach(const QString &family, database.families())
    {
        strList<<family;
    }
    strList<<"华文琥珀"<<"华文楷体"<<"华文行楷";
    ui->comboBox->addItems(strList);


    //功能3的表格
    ui->tableWidget_1->setColumnCount(3);

    QStringList header;
    header<<"序号"<<"图案编号"<<"描述";
    ui->tableWidget_1->setHorizontalHeaderLabels(header);
    ui->tableWidget_1->verticalHeader()->setVisible(false);



    update();
}

MainWindow::~MainWindow()
{
    delete ui;
}


//void MainWindow::paintEvent(QPaintEvent* event) {

//    QPainter painter(ui->widget);
//    QPen pen;
//    pen.setColor(Qt::blue);
//    painter.setPen(pen);

//    painter.drawText(10, 100, "This is subWidget");
//}



//保存文本按钮
void MainWindow::on_saveText_clicked()
{
    text1 = ui->textEdit->toPlainText();
    text1_radius = ui->spinBox->value();  //字体半径
    text1_size = ui->spinBox_2->value()/5;
    text1_angle = ui->spinBox_3->value();
    text1_spacing = ui->spinBox_7->value();

    //获取文字path
    qreal q = text1_angle+10*text1.length();
    qreal startAngle = -text1_angle;
    //qreal spanAngle = -(q-text1_angle);
    qreal spanAngle = text1_angle-text1_spacing;
    qreal rect_radius1 = (text1_radius+text1_size*2)*2;
    qreal rect_radius2 = (text1_radius-text1_size/2)*2;

    QPainterPath path, subPath;
    QRectF rect1(-rect_radius1/2,-rect_radius1/2,rect_radius1,rect_radius1);
    QRectF rect2(-rect_radius2/2,-rect_radius2/2,rect_radius2,rect_radius2);

    path.arcTo(rect1, startAngle, spanAngle);
    path.closeSubpath();
    subPath.addEllipse(rect2);
    path -= subPath;

    storetext temp_text = {text1,text1_radius,text1_size,text1_angle,text1_spacing,text1_family,path};
    if(all_store.text_sel == -1)   //没选中则导入
    {
        all_store.p_text.push_back(temp_text);
    }
    else
    {
        all_store.p_text[all_store.text_sel] = temp_text;
    }


    ui->widget->updacanvasPixmap(&all_store);  //更新窗口内的图形

    //清空原本框内的内容
    ui->textEdit->clear();
    ui->spinBox->setValue(0);
    ui->spinBox_2->setValue(0);
    ui->spinBox_3->setValue(0);
    ui->spinBox_7->setValue(0);
    all_store.text_sel = -1;
}



//更新文本框内的内容
void MainWindow::on_pushButton_2_clicked()
{
    if(all_store.text_sel == -1)   //没选中则清空所有内容
    {
        ui->textEdit->clear();
        ui->spinBox->setValue(0);
        ui->spinBox_2->setValue(0);
        ui->spinBox_3->setValue(0);
        ui->spinBox_7->setValue(0);
    }
    else    //选中则显示选中的内容
    {
        int sel = all_store.text_sel;
        ui->textEdit->setText(all_store.p_text[sel].text);
        ui->spinBox->setValue(all_store.p_text[sel].radius-100);
        ui->spinBox_2->setValue(all_store.p_text[sel].font_size*5);
        ui->spinBox_3->setValue(all_store.p_text[sel].angle);
        ui->spinBox_7->setValue(all_store.p_text[sel].word_spacing);
    }
}

//图案库的显示
void MainWindow::on_tableWidget_1_itemSelectionChanged()
{
    QTableWidgetItem *item[3];
    int i,nCount;
    qDebug()<<"all_store.p_layer:"<<all_store.p_layer.size();
    //for(i=0; i<all_store.picture_count; i++)
    for(i=0; i<all_store.p_layer.size(); i++)
    {
        nCount = ui->tableWidget_1->rowCount();
        ui->tableWidget_1->insertRow(nCount);

        //item[0] = new QTableWidgetItem(QString("%1").arg(i+1));
        //item[1] = new QTableWidgetItem(all_store.p_picture[i].p_text);
        int sel = all_store.p_layer[i].sel_picture;
        item[0] = new QTableWidgetItem(QString("%1").arg(i+1));
        item[1] = new QTableWidgetItem(QString("%1").arg(sel+1));
        item[2] = new QTableWidgetItem(all_store.p_picture[sel].p_text);
        ui->tableWidget_1->setItem(i, 0, item[0]);
        ui->tableWidget_1->setItem(i, 1, item[1]);
        ui->tableWidget_1->setItem(i, 2, item[2]);
    }
}

//点击图层库的选项，与图案库不同
void MainWindow::on_tableWidget_1_cellClicked(int row, int column)
{
    flag1 = 1;
    //qDebug()<<row<<column<<"图案库";
    QString strText = ui->tableWidget_1->item(row, column)->text();
    //qDebug()<<strText;
    cur_sel = row;    //选择当前文案库

    ui->spinBox_4->setValue(cur_sel);
//    ui->spinBox_5->setValue(all_store.p_picture[cur_sel].radius);
//    ui->spinBox_6->setValue(all_store.p_picture[cur_sel].angle);
    ui->spinBox_5->setValue(all_store.p_layer[cur_sel].radius);
    ui->spinBox_6->setValue(all_store.p_layer[cur_sel].angle);
    ui->scaleSpinBox->setValue(all_store.p_layer[cur_sel].scale);
    //qDebug()<<all_store.p_picture[cur_sel].radius<<all_store.p_picture[cur_sel].angle;
}

//纹理库表格刷新
void MainWindow::on_pushButton_3_clicked()
{
    this->on_tableWidget_1_itemSelectionChanged();
    ui->widget->updacanvasPixmap(&all_store);
    ui->spinBox_4->setValue(0);
    ui->spinBox_5->setValue(0);
    ui->spinBox_6->setValue(0);
    ui->scaleSpinBox->setValue(1.00);
}

//添加画好的图案到轮胎上
void MainWindow::on_Button_addTexture_clicked()
{
    //all_store.sel_picture.push_back(cur_sel);   //保存选中的图案编号
//    all_store.p_picture[cur_sel].radius = ui->spinBox_5->value();
//    all_store.p_picture[cur_sel].angle = ui->spinBox_6->value();
    all_store.p_layer[cur_sel].radius = ui->spinBox_5->value();
    all_store.p_layer[cur_sel].angle = ui->spinBox_6->value();
    all_store.p_layer[cur_sel].scale = ui->scaleSpinBox->value();  //改变图形比例
    //qDebug()<<all_store.p_picture[cur_sel].p_rect[0].x<<all_store.p_picture[cur_sel].p_rect[0].y;
    ui->widget->updacanvasPixmap(&all_store);
}


void MainWindow::on_Button_addNew_clicked()  //添加新图层界面
{
    texture_wrapper *wrapper = new texture_wrapper();
    wrapper->receiver(&all_store);
    wrapper->show();
}

void MainWindow::on_comboBox_currentIndexChanged(const QString &arg1)
{
     QString str = QString("当前选择项为 ")+arg1;
     text1_family = arg1;
     qDebug()<<text1_family;
}

void MainWindow::on_Button_delect_clicked()
{
    all_store.p_layer.erase(all_store.p_layer.begin()+cur_sel);
    this->on_tableWidget_1_itemSelectionChanged();
    ui->widget->updacanvasPixmap(&all_store);
    ui->spinBox_4->setValue(0);
    ui->spinBox_5->setValue(0);
    ui->spinBox_6->setValue(0);
    ui->scaleSpinBox->setValue(1.00);
}
